Check mime types in custom asset validation rules#3290
Check mime types in custom asset validation rules#3290jasonvarga merged 13 commits intostatamic:masterfrom
Conversation
|
Hmmm getting some unit test weirdness.. They run fine locally 😕 |
|
I know sometimes you see me merge PRs and you pounce on a new one. Just letting you know I'm working on this one next, so don't worry about fixing anything here yet 😄 |
|
Haha sure, I will leave this one alone 🐅 😉 |
# Conflicts: # src/Assets/Asset.php # tests/Assets/AssetTest.php
|
I've merged master into this and made a few tweaks. Thanks for this.
I agree that those methods could be updated to use mimetypes, but I'd to an actual mimetype check instead of comparing the guessed extensions. e.g. |
|
Nice! 🎉
Yeah I agree, that's better, more direct. |
Fixes #3277.
This PR is written on top of #3280 which provides a way to see if a key is missing, and regenerate the meta when needed.
Mimicking Laravel validation rules
The new asset validation rules in this PR mimic the behaviour of the original Laravel rules. The Laravel code for all this magic can be found in
Illuminate\Validation\Concerns\ValidatesAttributes.Laravel actually has two validation rules to check a file's mime type. One is called
mimesand, contrary to what you might expect, you should provide a list of extensions rather than mime types, like so:mimes:jpg,png. It doesn't use the actual file extension, instead it guesses the extension based on the mime type of the file.People found it confusing that you had to provide a list of extensions to this rule. So later a second rule was added called
mimetypes. This rule actually takes a list of mime types, e.g.mimetypes:image/jpg,image/png.Using the guessed extension elsewhere too
I wonder, should the
Assetclass use the guessed extension too now for all the extension-based tests likeisImage()andisVideo()?Let's say you rename your
image.jpgtoimage.radand you upload it to an asset field that expects an image. Meaning it has animagevalidator or something likemimes:jpg,png. The new validators will be perfectly fine with your renamed file, because the guessed extension will still bejpg. ButAsset.isImage()would currently return false, because it looks at the actual file extension, which is nowrad. This is inconsistent behaviour.